package push;

import com.aacar.utils.CommonStaticPath;
import com.aacar.utils.DateUtil;
import com.alibaba.fastjson.JSON;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.json.JSONArray;
import org.json.JSONObject;

import push.android.AndroidBroadcast;
import push.android.AndroidCustomizedcast;
import push.android.AndroidFilecast;
import push.android.AndroidGroupcast;
import push.android.AndroidUnicast;
import push.ios.*;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

public class PushUtils {
	private static Log logger = LogFactory.getLog(PushUtils.class);

	private static String appkey = "5ab3ef77f29d9803bc0007ac";
	private static String appMasterSecret = "df6enf5gkifq7qsojamvfwxxz9wvms4a";
	private static String timestamp = DateUtil.date2Str( DateUtil.defaultPattern2,DateUtil.addDateMinut(DateUtil.getNowDate(),24*60));
	private static PushClient client = new PushClient();

	private static String title = "爱爱洗车提醒您";


	public static void sendAndroidAndIOSUnicast(String deviceType, String deviceTokens, String text,String orderNo){
		if ("andriod_aacar_client".equals(deviceType)){
			logger.info("###### 推送android消息");
			sendAndroidUnicast(deviceTokens, text, orderNo);
		}else{
			logger.info("###### 推送IOS消息");
			sendIOSUnicast(deviceTokens, text, orderNo);
		}
	}



	public static void sendAndroidUnicast(String deviceTokens, String text,String orderNo){
		try {
			AndroidUnicast unicast = new AndroidUnicast(appkey, appMasterSecret);
			// TODO Set your device token
			unicast.setDeviceToken(deviceTokens);
			unicast.setTicker("Android unicast ticker");
			unicast.setTitle(title);
			unicast.setText(text);
			unicast.goAppAfterOpen();
			unicast.setExpireTime(timestamp);
			unicast.setDisplayType(AndroidNotification.DisplayType.NOTIFICATION);
			// TODO Set 'production_mode' to 'false' if it's a test device.
			// For how to register a test device, please see the developer doc.
			unicast.setProductionMode();
			// Set customized fields
			unicast.setExtraField("orderNo", orderNo);
			client.send(unicast);
		}catch (Exception e){
			e.printStackTrace();
		}
	}



	public static void sendIOSUnicast(String deviceTokens, String text,String orderNo){
		try {
			IOSUnicast unicast = new IOSUnicast("5ac830c3f43e48226700026a","9qxwt86ez7q5l4w6ugahtqzddwkvoici");
			// TODO Set your device token
			unicast.setDeviceToken(deviceTokens);
			JSONObject alertJson = new JSONObject();
			alertJson.put("body", text);
			alertJson.put("title", title);
			unicast.setAlert(alertJson);
			unicast.setCustomizedField("body", text);
			unicast.setCustomizedField("title", title);
			unicast.setCustomizedField("orderNo", orderNo);
			unicast.setCustomizedField("time", DateUtil.date2Str(DateUtil.defaultPattern2,DateUtil.getNowDate()));
			unicast.setExpireTime(timestamp);
			unicast.setBadge(0);
			unicast.setSound( "default");
			// TODO set 'production_mode' to 'true' if your app is under production mode
			if("sit".equals(CommonStaticPath.systemConf)){
				unicast.setTestMode();
			}else{
				unicast.setProductionMode();
			}

			// Set customized fields
			unicast.setCustomizedField("", "");
			unicast.getPostBody();
			client.send(unicast);
		}catch (Exception e){
			e.printStackTrace();
		}
	}

	


	public static void main(String[] args) {
//		try {
//			sendAndroidUnicast("AmIpyT52g9IHpfZyWd63ZGT606z5Bk0RrPdPnx1nHL1G","消息通知","通知内容");
//		} catch (Exception ex) {
//			ex.printStackTrace();
//		}
	}
	

}
